﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace StorageInterface.Dialogs
{
    /// <summary>
    /// Interaction logic for NewInPaymentDialog.xaml
    /// </summary>
    public partial class NewInPaymentDialog : Window
    {

        public NewInPaymentDialog(Window own)
        {
            this.Owner = own;
            InitializeComponent();
            var prtn = Data.GetAllPartners();
            foreach (var el in prtn)
                comboBox1.Items.Add(el);
        }

        public NewInPaymentDialog(Window own,  Order _order)
        {
            this.Owner = own;
            InitializeComponent();
            var prtn = Data.GetAllPartners();
            foreach (var el in prtn)
            {
                comboBox1.Items.Add(el);
                if (el.ID_Partner == _order.Partner.ID_Partner)
                    comboBox1.SelectedItem = el;
            }
            comboBox2.Items.Add(_order);
            comboBox2.SelectedItem = _order;
            comboBox2.IsEnabled = false;
            comboBox1.IsEnabled = false;
            textBox1.Text = _order.Summ.ToString();
            textBox1.IsReadOnly = true;

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            double _out_ = 0;
            if (comboBox1.SelectedItem == null ||
                comboBox2.SelectedItem == null ||
                //datePicker1.SelectedDate == null ||
                textBox1.Text == "" ||
                !double.TryParse(textBox1.Text, out _out_))
            {
                ErrorHandler.HandleError("Не все поля заполнены корректно!!!");
                return;
            }
            if (amfree < amneed)
            {
                ErrorHandler.HandleError("Ошибка при выполнение заявки!!!");
                return;
            }
            Data.InsertPayment(comboBox2.SelectedItem as Order, _out_, DateTime.Now);
            Close();


        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Partner p = comboBox1.SelectedItem as Partner;
            if (p == null)
                return;
            var pord = p.Orders.Where(el => el.State == "Выполняется").ToArray();
            comboBox2.Items.Clear();
            foreach (var el in pord)
                comboBox2.Items.Add(el);

        }

        double amfree = 0;
        double amneed = 0;
        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Order p = comboBox2.SelectedItem as Order;
            if (p == null)
            {
                textBox1.Clear();
                textBox2.Clear();
                textBox3.Clear();
                return;
            }
            amfree = Data.GetAvaibleAmount(p.Product);
            amneed = p.Amount;
            textBox1.Text = p.Summ.ToString();
            textBox2.Text = amfree.ToString();
            textBox3.Text = amneed.ToString();
         

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

    }
}
